<?php
namespace Modules\BlogAdmin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonSortRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Http\Requests\CommonDeleteAllRequest;
use Modules\BlogAdmin\Models\BlogArticle as BlogArticleModel;
use Modules\BlogAdmin\Models\BlogColumn as BlogColumnModel;
use Modules\BlogAdmin\Models\BlogLabel as BlogLabelModel;
use Modules\Admin\Models\Image as ImageModel;
use Modules\BlogAdmin\Models\BlogConfig as BlogConfigModel;
use Modules\Admin\Http\Controllers\CommonController;
class BlogArticleController extends CommonController
{
	/**
	 * @name  列表
	 */
    public function index()
    { 
		$column_list = (new BlogColumnModel)->lefts();
		$status = BlogArticleModel::common_status();
		$open_list = ['0'=>'否','1'=>'是'];
        return view('blogadmin::blog_article.index',compact('status','column_list','open_list'));
    }
	/**
	 * @name  列表数据
	 */
	 
    public function data(Request $request)
    {
	    $model = BlogArticleModel::query();
		$params = $request->all();
		$model = query_condition($model,$params,'title');
		if (isset($params['blog_column_id']) && $params['blog_column_id'] > 0){
            $model = $model->where('blog_column_id',$params['blog_column_id']);
        }
		if (isset($params['open']) && $params['open'] != ''){
			$model = $model->where('open',$params['open']);
		}
        $res = $model->with('image_one','column_to')
				->orderBy('sort','asc')
				->orderBy('id','desc')  
				->paginate($params['limit'])
				->toArray(); 
		$res['data'] = (new BlogArticleModel())->data_image($res['data']);	
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    { 
		$column_list = (new BlogColumnModel)->lefts();
		$label_list = BlogLabelModel::orderBy('id','desc')->get();
		return view('blogadmin::blog_article.create',compact('column_list','label_list'));
    }
	/**
	 * @name  添加数据处理 
	 */
    public function store(Request $request)
    {
		$data = $request->all();
		$data = (new ImageModel())->add_image($data,'file');
		if(!is_array($data)){
			return $data;
		};
		return (new BlogArticleModel())->add_create($data,route('blog_admin.article.index'));  
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$column_list = (new BlogColumnModel)->lefts();
		$label_list = BlogLabelModel::orderBy('id','desc')->get();
		$info = BlogArticleModel::with('image_one','column_to','label_to')->find($id)->toArray();
		if(count($info['label_to'])>0){
			$info['label_to'] = array_column($info['label_to'],'id');
		}else{  
			$info['label_to'] = [0];
		}
		return view('blogadmin::blog_article.edit',compact('info','column_list','label_list'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(Request $request,$id)
    {
		$data = $request->all();
		$data = (new ImageModel())->edit_image('blog_articles',$id,$data,'file');
		if(!is_array($data)){
			return $data;
		};
		return (new BlogArticleModel())->update_article($data,$id,route('blog_admin.article.index')); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new BlogArticleModel())->common_status_update($request->all(),$id,route('blog_admin.article.index')); 
    }
	/**
	 * @name  调整推荐状态数据处理
	 */
    public function open(CommonStatusRequest $request,$id)
    {
		return (new BlogArticleModel())->common_status_update($request->all(),$id,route('blog_admin.article.index')); 
    }
	/**
	 * @name  排序数据处理
	 */
    public function sorts(CommonSortRequest $request,$id)
    {
		return (new BlogArticleModel())->common_status_update($request->all(),$id,route('blog_admin.article.index'));  
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {
		$id = (new ImageModel())->del_image('blog_articles',$request->get('id'));
		if(!is_numeric($id)){
			return $id;
		};
		$model = new BlogArticleModel();
		$model->article_labels_del([$id]);
		return $model->common_destroy($id,route('blog_admin.article.index'));  
    }
	/**
	 * @name  批量删除数据处理
	 */
    public function destroy_all(CommonDeleteAllRequest $request)
    {
		$ids = (new ImageModel())->del_all_image('blog_articles',$request->get('ids'));
		if(!is_array($ids)){
			return $ids;
		};
		$model = new BlogArticleModel();
		$model->article_labels_del($ids);
		return $model->common_destroy($ids,route('blog_admin.article.index'));  
    }
	/**
	 * @name  文章推送
	 */
    public function push_url(Request $request)
    {
		$config = BlogConfigModel::find(1)->toArray();
		$res = baidu_t($config['baidu_url'],$config['baidu_key'],[route('blog.index.info',['id'=>$request->get('id')])]);
		if($res){
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => '推送成功！',
				'url'=>route('blog_admin.article.index')
			],200);
		}else{
			return response()->json([
				'status'=>0,
				'error' => '',
				'msg' => '推送失败！',
				'url'=>route('blog_admin.article.index')
			],201);
		}
    }
	
	
	
	
}
